import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wallpaper_app/components/windows/window_title_bar.dart';
import 'package:wallpaper_app/tools/custom_image_cache.dart';

class CacheClean extends StatefulWidget {
  const CacheClean({super.key});

  @override
  State<CacheClean> createState() => _CacheCleanState();
}

class _CacheCleanState extends State<CacheClean> {
  String cacheSize = '计算中';
  void getCacheSize() async {
    double size = await CustomCacheManager.fileSize();
    setState(() {
      // 保留两位小数
      cacheSize = size.toStringAsFixed(2);
    });
  }

  @override
  void initState() {
    getCacheSize();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).colorScheme;
    return Scaffold(
      body: Column(
        children: [
          WindowTitleBar(isBack: true),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Flex(
              direction: Axis.vertical,
              children: [
                Material(
                  color: theme.primaryContainer,
                  borderRadius: BorderRadius.circular(8),
                  clipBehavior: Clip.antiAlias,
                  child: ListTile(
                    leading: Icon(Icons.folder),
                    title: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('临时缓存目录'),
                        Text('D:\\custom_image_cache'),
                      ],
                    ),
                  ),
                ),
                SizedBox(height: 10),
                Material(
                  color: theme.primaryContainer,
                  borderRadius: BorderRadius.circular(8),
                  clipBehavior: Clip.antiAlias,
                  child: ListTile(
                    leading: Icon(Icons.file_download),
                    title: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('下载目录'),
                        Text('D:\\BoQiDown'),
                      ],
                    ),
                  ),
                ),
                SizedBox(height: 10),
                Material(
                  color: theme.primaryContainer,
                  borderRadius: BorderRadius.circular(8),
                  clipBehavior: Clip.antiAlias,
                  child: ListTile(
                    leading: Icon(Icons.clear_all),
                    title: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('清理缓存'),
                        Text('$cacheSize MB'),
                      ],
                    ),
                    // 右侧图标
                    trailing: Icon(Icons.arrow_forward_ios, size: 15),
                    onTap: () {
                      showCupertinoDialog(
                        //点击空白处取消
                        barrierDismissible: true,
                        context: context,
                        builder: (context) {
                          return CupertinoAlertDialog(
                            title: Text('提升'),
                            content: Padding(
                              padding: const EdgeInsets.only(top: 8),
                              child: Text('确认清除缓存吗？清理后将删除所有缓存文件。'),
                            ),
                            actions: [
                              CupertinoDialogAction(
                                child: Text('取消'),
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },
                              ),
                              CupertinoDialogAction(
                                child: Text('确认'),
                                onPressed: () async {
                                  setState(() {
                                    cacheSize = '0.0';
                                  });
                                  CustomCacheManager.clearCache();
                                  Navigator.of(context).pop();
                                },
                              ),
                            ],
                          );
                        },
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
